package resources 
{
	import app.AppSettings;
	import app.textures.TextureInfo;
	import app.textures.TextureInfoGroupe;
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.Graphics;
	import flash.display.Loader;
	import flash.display.Shape;
	import flash.utils.Dictionary;
	import furnitures.parts.Handle;
	/**
	 * ...
	 * @author Dmitriy Gaiduk
	 */
	public class Images 
	{
		private static var _instance:Images;
		private var _imagesDict:Dictionary;
		private static var _bmDictionary:Dictionary;
		
		public static const HANDLE_HORIZ:String = "HANDLE_HORIZ";
		public static const HANDLE_VERT:String = "HANDLE_VERT";
		
		public static const GRID:String = "GRID";
		public static const GLASS:String = "GLASS";
		
		public function Images(singeltonLock:SingeltonLockImages) 
		{
			if (_instance != null)
			{
				throw new Error("Images already exist");
			}
			_imagesDict = new Dictionary();
			_bmDictionary = new Dictionary();
		}
		
		public static function getInstance():Images
		{
			if (_instance == null)
			{
				_instance = new Images(new SingeltonLockImages());
			}
			return _instance;
		}
		
		public static function getTile():DisplayObject
		{
			return new TileMovie();
		}
		
		
		public static function getImageBitmap(imageName:String):BitmapData
		{
			var bmData:BitmapData = _bmDictionary[imageName] as BitmapData;
			
			if (bmData == null)
			{
				bmData = createBitmap(imageName);
			}
			
			return bmData;
		}
		
		private static function createBitmap(imageName:String):BitmapData
		{
			var imgObj:DisplayObject;
			
			switch(imageName)
			{
				case HANDLE_HORIZ:
					 imgObj = new HandleHorizMovie();
					break;
				case HANDLE_VERT:
					 imgObj = new HandleVeritcalMovie();
					break;
				case GRID:
					 imgObj = new GridMov();
					break;
				case GLASS:
					 imgObj = new GlassMov();
					break;
			}
			imgObj.scaleX = imgObj.scaleY = AppSettings.SCALE_FACTOR;
			
			var bmData:BitmapData;
			
			if (imgObj != null)
			{
				bmData = new BitmapData(imgObj.width, imgObj.height, true, 0xFF0000);
				bmData.draw(imgObj, null, null, null, null, true);
			}
			
			return bmData;
		}
		
		public function getDoorBitmap(doorType:int):BitmapData
		{
			var shape:Shape = new Shape();
			var gr:Graphics = shape.graphics;
			
			gr.beginFill(0xCCCCCC);
			gr.lineStyle(1, 0x000000);
			var width:Number = 15;
			var height:Number = 3;
			switch(doorType)
			{
				case Handle.HANDLE_HORIZONTAL:
						gr.drawRect(0, 0, width, height);
					break;
				case Handle.HANDLE_VERTICAL:
						gr.drawRect(0, 0, height, width);
					break;
				default:
						gr.drawCircle(0, 0, 5);
					break;
			}
			var handleBM:BitmapData = new BitmapData(shape.width, shape.height, true, 0xF0000);
			handleBM.draw(shape);
			return handleBM;
			
		}
		
		public function getImage(imageName:String):DisplayObject
		{
			var img:DisplayObject = _imagesDict[imageName] as DisplayObject;
			if (img == null)
			{
				//load img
				var loader:ImageLoader = new ImageLoader(imageName);
				img = loader.run();
				_imagesDict[imageName] = img;
			}
			
			if (img == null)
			{
				img = new Shape();
			}
			if (img.parent != null)
			{
				img.parent.removeChild(img);
			}
			return img;
		}
		
		public static function getBitmapData(img:Class):BitmapData
		{
			var pic:DisplayObject = new img() as DisplayObject;
			if (pic == null)
			{
				return null;
			}
			var bm:BitmapData = new BitmapData(pic.width, pic.height, false, 0xFF0000);
			bm.draw(pic, null, null, null, null, true);
			return bm;
		}
		
		public function getTexturePreview(textureInfo:TextureInfo, groupe:TextureInfoGroupe):DisplayObject
		{
			var img:DisplayObject = _imagesDict[textureInfo.previewURL] as DisplayObject;
			//trace("image from dictionary: " + img + " url: " + textureInfo.previewURL);
			if (img == null)
			{
				//load img
				//var loadSource:Boolean = groupe.textureType == TextureInfoGroupe.TEXTURE_TYPE_BITMAP? true : false;
				var loadSource:Boolean = false;
				var loader:TextureLoader = new TextureLoader(textureInfo, 
															groupe.previewWidth, 
															groupe.previewHeight,
															loadSource);
				img = loader.run();
				_imagesDict[textureInfo.previewURL] = img;
			}
			if (img == null)
			{
				img = new Shape();
			}
			if (img.parent != null)
			{
				img.parent.removeChild(img);
			}
			
			return img;
		}
		
		public function loadTexture(textureInfo:TextureInfo):void
		{
			
			textureInfo.texturePreview;
		}
		
		
	}

}

class SingeltonLockImages
	{
		public function SingeltonLockImages()
		{
			
		}
	}